package com.fastadmin.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import com.fastadmin.mapper.impl.UserMapper;
import com.fastadmin.pojo.User;
import com.fastadmin.service.UserService;
import com.zaxxer.hikari.HikariDataSource;
import lombok.RequiredArgsConstructor;
import org.apache.lucene.document.StringField;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.sql.SQLException;

@Controller
@RequiredArgsConstructor
public class LoginController extends BaseController {
    private final UserMapper userMapper;
    private final UserService userService;

    /**
     * 登录
     *
     * @return
     */
    @GetMapping("/login")
    public String login() {
        return "login";
    }
    @GetMapping("/captcha")
    @ResponseBody
    public void captcha(HttpServletResponse response, HttpServletRequest request) throws IOException {
        response.setHeader("Cache-Control", "no-store");
        response.setHeader("Pragma", "no-cache");
        response.setDateHeader("Expires", 0);
        //设置响应类型，告知浏览器输出的是图片
        response.setContentType("image/jpeg");
        //设置HttpOnly属性,防止Xss攻击
        response.setHeader("Set-Cookie", "name=value; HttpOnly");
        ServletOutputStream servletOutputStream = null;
        ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream();
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 100);
        lineCaptcha.setBackground(Color.white);
        BufferedImage image = lineCaptcha.getImage();
        ImageIO.write(image, "png", jpegOutputStream);
        byte[] captchaChallengeAsJpeg = jpegOutputStream.toByteArray();
        servletOutputStream = response.getOutputStream();
        servletOutputStream.write(captchaChallengeAsJpeg);
        servletOutputStream.flush();
        servletOutputStream.close();
    }


    @GetMapping(value = "/regist")
    @ResponseBody
    public void regist(String username, String password) throws SQLException {

    }
}
